<?php

    /*
     *  文件下载控制器
     *  @构造函数       检查SESSION
     *  @dbConnect      数据库连接，设置编码，选择库
     *  @dbClose        关闭数据库连接
     *  @getDetail      从数据库获取到要下载的信息的资源描述符
     *  @writeFile      将资源描述符fetch成关联数组，并implode成字符串之后写入到文件
     *  @download       下载文件
     * */

    require_once('BaseCtrl.php');

    //define('DOWNLOAD_PATH', 'D:\PHP\PHPTutorial\WWW\UXIN\resume\public\\');

    class DownloadCtrl extends CtrlBase {

        private $db;
        private $queryResult;
        private $file;


        public function __construct() {

            parent::__construct();

            parent::checkSession();

        }

        public function dbConnect() {

            //  数据库连接
            $this->db = mysql_connect('localhost', 'root', 'zpc456..');

            if(!$this->db) {

                echo "<script>alert('Database connect failed!')</script>";

                return false;

            }

            //  设置编码
            if(!mysql_query('set names utf8', $this->db)) {

                echo "<script>alert('Database set encoding failed!')</script>";
                return false;

            }

            //  选择数据库
            if(!mysql_select_db('resume', $this->db)) {

                echo "<script>alert('Select db failed!')</script>";
                return false;

            }

            return true;

        }


        //  关闭数据库连接
        public function dbClose() {

            if(!$this->db) {

                return true;

            }

            $flag = mysql_close($this->db);

            if(!$flag) {

                echo "<script>alert('mysql close error!')</script>";

                return false;

            }

            return true;

        }


        //  从数据库获取详情
        public function getDetail() {

            $this->dbConnect();

            $this->queryResult = mysql_query('select * from resume_info', $this->db);

            if(!$this->queryResult) {

                echo "<script>alert('mysql query error')</script>";

                return false;

            }

            $this->dbClose();

            return true;

        }


        //  写入文件
        public function writeFile() {

            //  打开文件
            $this->fileOpen('w+');

            if(!$this->file) {

                echo "<script>alert('file open error')</script>";

                return false;

            }

            //  将从数据库获取到的信息写入到文件
            while($rowData = mysql_fetch_assoc($this->queryResult)) {

                $rowStr = implode(',', $rowData) . "\r\n";

                fwrite($this->file, $rowStr);

            }

            //  关闭文件
            $this->fileClose();

            return true;

        }


        //  关闭文件
        public function fileClose() {

            if($this->file) {

                fclose($this->file);

                return true;

            }

            return false;

        }


        //  打开文件
        public function fileOpen($mode) {

            $this->file = fopen(DOWNLOAD_PATH . 'detail.txt', $mode);

            if(!$this->file) {

                return false;

            }

            return true;

        }


        //  调用MODEL中下载文件的方法
        public function downloadDetail() {

            //  获取详情
            $flag = $this->getDetail();

            if(!$flag) {

                echo "<script>alert('getDetail error in download!')</script>";

                return false;

            }

            //  写入文件
            $flag = $this->writeFile();

            if(!$flag) {

                echo "<script>alert('writeFile error in download!')</script>";

                return false;

            }

            //  指定下载规则
            Header('Content-type:application/octet-stream');

            Header('Accept-Range:byte');

            Header('Accept-Length:' . filesize(DOWNLOAD_PATH . 'detail.txt'));

            Header('Content-Disposition:attachment;filename=detail.txt');

            $this->fileOpen('r');

            echo fread($this->file, filesize(DOWNLOAD_PATH . 'detail.txt'));

            $this->fileClose();

            return true;

        }

    }

?>